<?php
use tpfcore\Core;
?>
<!doctype html>
<html>

<head>
    <title>编辑商品</title>
    <meta charset="utf-8">
    <!-- Set render engine for 360 browser -->
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- HTML5 shim for IE8 support of HTML5 elements -->
    <!--[if lt IE 9]>
		<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js">
		</script>
	<![endif]-->
    <link href="{$admin_assets_path}/layui/css/layui.css" rel="stylesheet" />
    <link href="{$admin_assets_path}/css/theme.min.css" rel="stylesheet">
    <link href="{$admin_assets_path}/css/simplebootadmin.css" rel="stylesheet">
    <link href="{$admin_assets_path}/css/default.css" rel="stylesheet" />
    <link href="{$admin_assets_path}/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <style>
    .length_3 {
        width: 180px;
    }

    form .input-order {
        margin-bottom: 0px;
        padding: 3px;
        width: 40px;
    }

    .table-actions {
        margin-top: 5px;
        margin-bottom: 5px;
        padding: 0px;
    }

    .table-list {
        margin-bottom: 0px;
    }
    .table-label label{display: inline-block;}
    #goods_imgs{overflow: hidden; text-align: center;}
    #goods_imgs img{width: 100%; height: auto;max-height: 120px; }
    #goods_imgs .item{width: 120px;float: left; border:1px #eaeaea solid; padding: 4px; margin-right:10px; margin-bottom: 10px; height: 170px;}
    #goods_imgs .item a:nth-child(2){display: block; height: 120px;line-height: 120px;}
    #goods_imgs input{width: 100%; box-sizing: border-box; height: 30px;}
    </style>
    <!--[if IE 7]>
		<link rel="stylesheet" href="{$admin_assets_path}/css/font-awesome-ie7.min.css">
	<![endif]-->
    <script type="text/javascript">
    //全局变量
    var GV = {
        WEB_ROOT: "",
        DIMAUB: "/",
        JS_ROOT: "/theme/backend/assets/js/",
        TOKEN: ""
    };
    </script>
    <!-- Le javascript==================================================-
	-> <!-- Placed at the end of the document so the pages load faster -->
    <script src="{$admin_assets_path}/js/jquery.js"></script>
    <script src="{$admin_assets_path}/js/wind.js"></script>
    <script src="{$admin_assets_path}/js/bootstrap.min.js"></script>
    <link href="{$admin_assets_path}/kindeditor/themes/default/default.css" rel="stylesheet" />
    <script src="{$admin_assets_path}/kindeditor/kindeditor-min.js"></script>
    <script src="{$admin_assets_path}/kindeditor/lang/zh_CN.js"></script>
    <script src="{$admin_assets_path}/layui/layui.all.js"></script>
    <script type="text/javascript">
    function mul(arg1, arg2) {
        var r1 = arg1.toString(), r2 = arg2.toString(), m, resultVal, d = arguments[2];
        m = (r1.split(".")[1] ? r1.split(".")[1].length : 0) + (r2.split(".")[1] ? r2.split(".")[1].length : 0);
        resultVal = Number(r1.replace(".", "")) * Number(r2.replace(".", "")) / Math.pow(10, m);
        return typeof d !== "number" ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)));
    }
    function initOpAttr(){
        addAttr();
        removeAttr();
    }
    function addAttr(){
        $(".addAttr").unbind("click").click(function(){
            var currObj = $(this).parents(".control-group"),obj = currObj.clone();
            obj.html(obj.html().replace(/class="addAttr"(.*)(\[)(\+])/i, "class='remoteAttr'$1$2-]&nbsp;"));
            currObj.after(obj);
            obj.find(".controls").children().eq(1).val('');
            obj.find(".controls").children().eq(2).val('');
            removeAttr();
        });
    }
    function removeAttr(){
        $(".remoteAttr").unbind("click").click(function(){
            $(this).parents(".control-group").remove();
        });
    }

    function getAttrByTypeId(type_id){
        $.ajax({
            type:"POST",
            url:"{:url('mall/GoodsAttribute/getByTypeId')}",
            dateType:"html",
            data:{"type_id":type_id,"goods_id":{$list.id}},
            success:function(data){
                $("#attribute_lists").html(data);
                initOpAttr();
            }
        });
    }

    $(function(){
        initOpAttr();
        getAttrByTypeId({$list.goods_type});
        $("#is_promote").click(function(){
            if($(this).attr("checked")){
                $("#promote_price").attr("disabled",false);
                $("#promote_time").show();
            }else{
                $("#promote_price").attr("disabled",true);
                 $("#promote_time").hide();
            }
        });
        $("#goods_type").change(function(){
            var type_id = $(this).val();
            if($(this).val()==0){
                $("#attribute_lists").empty();
            }else{
                getAttrByTypeId(type_id);
            }
        });
        $("#marketCount").click(function(){
            console.log(1111111);
            var ratio = {$listShopConfig.options.market_price_ratio|default=1};
            var price = parseInt($("#price").val());
            console.log(price*ratio+":::"+typeof price);
            if(isNaN(price) || typeof price !== 'number' || price<0){
                return false;
            }

            $("#market_price").val(mul(price,ratio));
        });
    });
    
    KindEditor.ready(function(K) {
        var editor = K.editor({
            allowFileManager : true,
            dirName : 'data/uploads',
            uploadJson:'{:Core::addons_url("kindeditor://Editor/pictureUpload")}',
            fileManagerJson:'{:url("Kindeditor/Kindeditor/file_manage")}'
        });
        K('#uploadGoodsImg').click(function() {
            editor.loadPlugin('image', function() {
                editor.plugin.imageDialog({
                    viewType : 'LIST',
                    dirName : 'data/uploads',
                    clickFn : function(url, title, width, height, border, align) {
                        console.log(url);
                        editor.hideDialog();
                        $("#goods_img").val(url);
                        $("#goods_img_show").attr("src",url).show();
                    }
                });
            });
        });
        K('#uploadGoodsThumb').click(function() {
            editor.loadPlugin('image', function() {
                editor.plugin.imageDialog({
                    viewType : 'LIST',
                    dirName : 'data/uploads',
                    clickFn : function(url, title, width, height, border, align) {
                        console.log(url);
                        editor.hideDialog();
                        $("#goods_thumb").val(url);
                        $("#goods_thumb_show").attr("src",url).show();
                    }
                });
            });
        });
        K('#uploadButton').click(function() {
            editor.loadPlugin('image', function() {
                editor.plugin.imageDialog({
                    viewType : 'LIST',
                    dirName : 'data/uploads',
                    clickFn : function(url, title, width, height, border, align) {
                        console.log(url);
                        editor.hideDialog();
                        var item = $("#item_tpl").find(".item").clone();
                        item.find("img").attr("src",url);
                        item.find(".url").val(url);
                        item.find(".desc").val(title);
                        $("#goods_imgs").append(item);
                        remove();
                    }
                });
            });
        });
        K('#multUploadButton').click(function() {
            editor.loadPlugin('multiimage', function() {
                editor.plugin.multiImageDialog({
                    clickFn : function(urlList) {
                        editor.hideDialog();
                        K.each(urlList, function(i, data) {
                            console.log(data);
                            var item = $("#item_tpl").find(".item").clone();
                            item.find("img").attr("src",data.img_url);
                            item.find(".url").val(data.img_url);
                            $("#goods_imgs").append(item);
                            remove();
                        });
                    }
                });
            });
        });
    }); 
    </script>
</head>

<body>
    <div class="wrap js-check-wrap">
        <ul class="nav nav-tabs">
            <li class="active"><a href="#A" data-toggle="tab">基本信息</a></li>
            <li><a href="#B" data-toggle="tab">商品描述</a></li>
            <li><a href="#C" data-toggle="tab">其它信息</a></li>
            <li><a href="#D" data-toggle="tab">商品属性</a></li>
            <li><a href="#E" data-toggle="tab">商品图片</a></li>
            <li style="float: right;"><a href="{:url('mall/Goods/index')}">商品列表</a></li>
        </ul>
        <form class="form-horizontal js-ajax-form" action="" method="post">
            <input type="hidden" name="id" value="{$list.id}" />
            <fieldset>
                <div class="tabbable">
                    <div class="tab-content">
                        <div class="tab-pane active" id="A">
                            <fieldset>
                                <div class="control-group">
                                    <label class="control-label">商品名称</label>
                                    <div class="controls">
                                        <input type="text" name="goods_name" style="width: 500px;" placeholder="商品名称" value="{$list.goods_name|default=''}"><span class="form-required">*</span>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">商品货号</label>
                                    <div class="controls">
                                        <input type="text" name="goods_sn" value="{$list.goods_sn|default=''}" placeholder="商品货号"><span class="form-required"> * 如果您不输入商品货号，系统将自动生成一个唯一的货号</span>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">商品分类</label>
                                    <div class="controls">
                                        <select name="category_id">
                                            <option value="0">选择商品分类</option>
                                            {$categorys}
                                        </select>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">上传商品图片</label>
                                    <div class="controls">
                                        <input type="button" id="uploadGoodsImg" value="上传商品图片" />
                                        <input type="hidden" name="goods_img" id="goods_img" value="{$list.goods_img}" />
                                        {empty name="list.goods_img"}
                                        <img src="" id="goods_img_show" style="display: none; width: 100px; height: 100px;">
                                        {else/}
                                        <img src="{$list.goods_img}" id="goods_img_show" style="width: 100px; height: 100px;">
                                        {/empty}
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">上传商品缩略图</label>
                                    <div class="controls">
                                        <input type="button" id="uploadGoodsThumb" value="上传商品缩略图" />
                                        <input type="hidden" name="goods_thumb" id="goods_thumb" value="{$list.goods_thumb}" />
                                        {empty name="list.goods_thumb"}
                                        <img src="" id="goods_thumb_show" style="display: none; width: 100px; height: 100px;">
                                        {else/}
                                        <img src="{$list.goods_thumb}" id="goods_thumb_show" style="width: 100px; height: 100px;">
                                        {/empty}
                                        <label><input type="checkbox" name="auto_thumb" value="1"> 自动生成缩略图</label>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">商品品牌</label>
                                    <div class="controls">
                                        <select name="brand_id">
                                            <option value="0">请选择品牌</option>
                                            {volist name="listGoodsBrand" id="vo"}
                                            <option value="{$vo.id}" {eq name="vo.id" value="$list.brand_id"}selected="selected"{/eq}>{$vo.name}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">本店售价</label>
                                    <div class="controls">
                                        <input type="text" name="goods_price" id="price" value="{$list.goods_price|default='0'}" placeholder="0.00"><span class="form-required"> <input type="button" id="marketCount" value="按市场价计算" /> *</span>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">市场售价</label>
                                    <div class="controls">
                                        <input type="text" name="market_price" id="market_price" value="{$list.market_price|default='0'}">
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">虚拟销量</label>
                                    <div class="controls">
                                        <input type="text" name="virtual_sales" value="{$list.virtual_sales|default='0'}">
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">赠送消费积分数</label>
                                    <div class="controls">
                                        <input type="text" name="give_integral" value="{$list.give_integral|default='0'}"> <span>购买该商品时赠送消费积分数,0表示不赠送</span>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">最多使用积分</label>
                                    <div class="controls">
                                        <input type="text" name="integral" value="{$list.integral|default='0'}"> <span>购买该商品时最多能使用多少积分,0表示不能使用积分</span>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">促销价 <input type="checkbox" name="is_promote" id="is_promote" value="1" {eq name="$list.is_promote" value="1"}checked="checked"{/eq}/></label>
                                    <div class="controls">
                                        <input type="text" name="promote_price" id="promote_price" value="{$list.promote_price|default='0'}" {eq name="$list.is_promote" value="0"}disabled="disabled"{/eq}/>
                                    </div>
                                </div>
                                <div class="control-group" id="promote_time" {eq name="$list.is_promote" value="0"}style="display: none;"{/eq}>
                                    <label class="control-label">促销日期</label>
                                    <div class="controls">
                                        <input type="text" id="promote_start_date" name="promote_start_date" value="{$list.promote_start_date|default='0'}"> - <input type="text" id="promote_end_date" name="promote_end_date" value="{$list.promote_end_date|default='0'}">
                                    </div>
                                </div>
                            </fieldset>
                        </div>
                        <div class="tab-pane" id="B">
                            <div class="control-group">
                                <label class="control-label">商品关键词</label>
                                <div class="controls">
                                    <input type="text" name="keywords" value="{$list.keywords|default=''}" placeholder="商品关键词,多个词用英文逗号分隔" style="width: 600px;" />
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">商品简单描述</label>
                                <div class="controls">
                                    <textarea name="short_describe" style="width: 600px; height: 80px;" placeholder="商品简单描述,不写将取出详情描述前200个字">{$list.short_describe|default=""}</textarea>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label">商品详细描述</label>
                                <div class="controls">{:Core::hook('ueditor',['name'=>'content','width'=>750,'height'=>200,'default_str'=>$list.content])}</div>
                            </div>
                        </div>
                        <div class="tab-pane" id="C">
                            <fieldset>
                                <div class="control-group">
                                    <label class="control-label">商品重量</label>
                                    <div class="controls">
                                        <input type="text" name="weight" value="{$list.weight|default=''}" placeholder="商品重量"><span class="form-required"> * 按kg来计算，例如500k为0.5kg</span>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">商品库存数量</label>
                                    <div class="controls">
                                        <input type="text" name="stock" value="{$list.stock|default='1'}" placeholder="商品库存数量"><span class="form-required"> * 库存在商品为虚货或商品存在货品时为不可编辑状态，库存数值取决于其虚货数量或货品数量</span>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">库存警告数量</label>
                                    <div class="controls">
                                        <input type="text" name="warn_number" value="{$list.warn_number|default='1'}" placeholder="库存警告数量">
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">加入推荐</label>
                                    <div class="controls">
                                        <input type="checkbox" name="is_recommend" value="1" {eq name="list.is_recommend" value="1"}checked{/eq}/>推荐 &nbsp;&nbsp;
                                        <input type="checkbox" name="is_new" value="1" {eq name="list.is_new" value="1"}checked{/eq}/>新品 &nbsp;&nbsp;
                                        <input type="checkbox" name="is_hot" value="1" {eq name="list.is_hot" value="1"}checked{/eq}/>热销
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">上架状态</label>
                                    <div class="controls">
                                        <input type="checkbox" name="is_on_sale" value="1" {eq name="list.is_on_sale" value="1"}checked{/eq}><span class="form-required"> *打勾表示允许销售，否则不允许销售</span>
                                    </div>
                                </div>
                            </fieldset>
                        </div>
                        <div class="tab-pane" id="D">
                            <fieldset>
                                <div class="control-group">
                                    <label class="control-label">商品类型</label>
                                    <div class="controls">
                                        <select name="goods_type" id="goods_type">
                                            <option value="0">请选择商品类型</option>
                                            {volist name="listGoodsType" id="vo"}
                                            <option value="{$vo.id}" {eq name="vo.id" value="$list.goods_type"}selected="selected"{/eq}>{$vo.name}</option>
                                            {/volist}
                                        </select>
                                        <span> 请选择商品的所属类型，进而完善此商品的属性</span>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset id="attribute_lists">
                                
                            </fieldset>
                        </div>
                        <div class="tab-pane" id="E">
                            <fieldset>
                                <div class="control-group">
                                    <div id="goods_imgs">
                                        {volist name="listPhoto" id="vo"}
                                        <div class="item">
                                            <a href="javascript:;" class="remove-img" data-id="{$vo.id}">[-]</a>
                                            <a href="{$vo.url}" target="_blank"><img src="{$vo.url}"></a>
                                            <input type="text" class="desc" name="old_desc[{$vo.id}]" value="{$vo.desc}" placeholder="图片描述">
                                        </div>
                                        {/volist}
                                    </div>
                                    <div style="text-align: center;">
                                        <div>最多能上传{$listShopConfig.options.max_upload_goods_photo|default=8}张图片</div>
                                        <input type="button" id="uploadButton" value="单张图片上传" />
                                        <input type="button" id="multUploadButton" value="批量上传图片" />
                                    </div>
                                </div>
                            </fieldset>
                            <div style="display: none;" id="item_tpl">
                                <div class="item">
                                    <a href="javascript:;" class="remove">[-]</a>
                                    <a href="#" target="_blank"><img src=""></a>
                                    <input type="text" class="desc" name="desc[]" placeholder="图片描述" />
                                    <input type="hidden" class="url" name="url[]"/>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="form-actions">
                    <button type="submit" class="btn btn-primary js-ajax-submit">保存</button>
                </div>
            </fieldset>
        </form>
    </div>
    <script src="{$admin_assets_path}/js/common.js"></script>
    <script type="text/javascript">
    var layer = layui.layer;
    $("#goods_imgs .remove-img").click(function(){
        $this = $(this);
        if(confirm("你确定要删除该图片吗")){
            $.ajax({
                type:"post",
                data:{"id":$(this).attr("data-id")},
                dataType:"json",
                url:"{:url('mall/GoodsPhoto/del')}",
                success:function(data){
                    if(data.code == 0){  // 成功
                        layer.msg(data.msg, {icon: 1});
                        $this.parent().remove();
                    }else{
                        layer.msg(data.msg, {icon: 2});
                    }
                }
            });
        }
    });

    function remove(){
        $("#goods_imgs .remove").unbind("click").click(function(){
            var $this=$(this);
            var url = $(this).parent().find("img").attr("src");
            if(confirm("你确定要删除该图片吗")){
                $.ajax({
                    type:"post",
                    data:{"url":url},
                    dataType:"json",
                    url:"{:url('mall/GoodsPhoto/del')}",
                    success:function(data){
                        if(data.code == 0){  // 成功
                            layer.msg(data.msg, {icon: 1}); 
                            $this.parent().remove();
                        }else{
                            layer.msg(data.msg, {icon: 2});
                        }
                    }
                });
            }
        });
    }
    remove();

layui.use('laydate', function(){
  var laydate = layui.laydate;
  //执行一个laydate实例
  laydate.render({
    elem: '#promote_start_date', //指定元素
    {eq name="$list.is_promote" value="1"}
    value:"{:date('Y-m-d',$list.promote_start_date)}"
    {/eq}
  });
  laydate.render({
    elem: '#promote_end_date', //指定元素
    {eq name="$list.is_promote" value="1"}
    value:"{:date('Y-m-d',$list.promote_end_date)}"
    {/eq}
  });
});

    </script>
</body>

</html>